﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using InTREPWP.ViewModels;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;

namespace InTREPWP.Views
{
    public partial class LoginPage : BasePage
    {
        // Constructor
        public LoginPage()
        {
            InitializeComponent();

            txtUsername.Text = Settings.IMEI.Value;
        }

        private void btnEntrar_Click(object sender, RoutedEventArgs e)
        {
            LoginViewModel loginvm = new LoginViewModel{
                IMEI = txtUsername.Text,
                Password = txtPassword.Password
            };

            String json = JsonConvert.SerializeObject(loginvm);

            IntrepService service = new IntrepService();
            Boolean loginvalido = service.Autenticar(json);

            if (loginvalido)
                this.NavigationService.Navigate(new Uri("/Views/Embarques.xaml", UriKind.Relative));
            else
            {
                MessageBox.Show("Aparelho não cadastrado ou senha incorreta!", "Erro ao autenticar!", MessageBoxButton.OK);
                txtPassword.Password = String.Empty;
                txtPassword.Focus();
            }
        }

        private void ApplicationBarBtnConfigurar_Click(object sender, EventArgs e)
        {
            this.NavigationService.Navigate(new Uri("/Views/ConfigurarLogin.xaml", UriKind.Relative));
        }


        private void ApplicationBarMenuSobre_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/Sobre.xaml", UriKind.Relative));

        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            txtUsername.Text = Settings.IMEI.Value;
        }

    }
}